package org.apache.qpid.management.common.sasl;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.apache.harmony.javax.security.auth.callback.Callback;
import org.apache.harmony.javax.security.auth.callback.CallbackHandler;
import org.apache.harmony.javax.security.auth.callback.NameCallback;
import org.apache.harmony.javax.security.auth.callback.PasswordCallback;
import org.apache.harmony.javax.security.auth.callback.UnsupportedCallbackException;
import org.apache.harmony.javax.security.sasl.SaslClient;
import org.apache.harmony.javax.security.sasl.SaslException;

public class PlainSaslClient
  implements SaslClient
{
  private boolean completed = false;
  private CallbackHandler cbh;
  private String authorizationID;
  private String authenticationID;
  private byte[] password;
  private static byte SEPARATOR = 0;

  public PlainSaslClient(String paramString, CallbackHandler paramCallbackHandler)
    throws SaslException
  {
    this.cbh = paramCallbackHandler;
    Object[] arrayOfObject = getUserInfo();
    this.authorizationID = paramString;
    this.authenticationID = ((String)arrayOfObject[0]);
    this.password = ((byte[])arrayOfObject[1]);
    if ((this.authenticationID == null) || (this.password == null))
      throw new SaslException("PLAIN: authenticationID and password must be specified");
  }

  public byte[] evaluateChallenge(byte[] paramArrayOfByte)
    throws SaslException
  {
    if (this.completed)
      throw new IllegalStateException("PLAIN: authentication already completed");
    this.completed = true;
    try
    {
      byte[] arrayOfByte1 = this.authorizationID == null ? null : this.authorizationID.getBytes("UTF8");
      byte[] arrayOfByte2 = this.authenticationID.getBytes("UTF8");
      byte[] arrayOfByte3 = new byte[this.password.length + arrayOfByte2.length + 2 + (arrayOfByte1 != null ? arrayOfByte1.length : 0)];
      int i = 0;
      if (arrayOfByte1 != null)
      {
        System.arraycopy(arrayOfByte1, 0, arrayOfByte3, 0, arrayOfByte1.length);
        i = arrayOfByte1.length;
      }
      arrayOfByte3[(i++)] = SEPARATOR;
      System.arraycopy(arrayOfByte2, 0, arrayOfByte3, i, arrayOfByte2.length);
      i += arrayOfByte2.length;
      arrayOfByte3[(i++)] = SEPARATOR;
      System.arraycopy(this.password, 0, arrayOfByte3, i, this.password.length);
      clearPassword();
      return arrayOfByte3;
    }
    catch (UnsupportedEncodingException localUnsupportedEncodingException)
    {
      throw new SaslException("PLAIN: Cannot get UTF-8 encoding of ids", localUnsupportedEncodingException);
    }
  }

  public String getMechanismName()
  {
    return "PLAIN";
  }

  public boolean hasInitialResponse()
  {
    return true;
  }

  public boolean isComplete()
  {
    return this.completed;
  }

  public byte[] unwrap(byte[] paramArrayOfByte, int paramInt1, int paramInt2)
    throws SaslException
  {
    if (this.completed)
      throw new IllegalStateException("PLAIN: this mechanism supports neither integrity nor privacy");
    throw new IllegalStateException("PLAIN: authentication not completed");
  }

  public byte[] wrap(byte[] paramArrayOfByte, int paramInt1, int paramInt2)
    throws SaslException
  {
    if (this.completed)
      throw new IllegalStateException("PLAIN: this mechanism supports neither integrity nor privacy");
    throw new IllegalStateException("PLAIN: authentication not completed");
  }

  public Object getNegotiatedProperty(String paramString)
  {
    if (this.completed)
    {
      if (paramString.equals("javax.security.sasl.qop"))
        return "auth";
      return null;
    }
    throw new IllegalStateException("PLAIN: authentication not completed");
  }

  private void clearPassword()
  {
    if (this.password != null)
    {
      for (int i = 0; i < this.password.length; i++)
        this.password[i] = 0;
      this.password = null;
    }
  }

  public void dispose()
    throws SaslException
  {
    clearPassword();
  }

  protected void finalize()
  {
    clearPassword();
  }

  private Object[] getUserInfo()
    throws SaslException
  {
    try
    {
      String str1 = "PLAIN authentication id: ";
      String str2 = "PLAIN password: ";
      NameCallback localNameCallback = new NameCallback("PLAIN authentication id: ");
      PasswordCallback localPasswordCallback = new PasswordCallback("PLAIN password: ", false);
      this.cbh.handle(new Callback[] { localNameCallback, localPasswordCallback });
      String str3 = localNameCallback.getName();
      char[] arrayOfChar = localPasswordCallback.getPassword();
      byte[] arrayOfByte;
      if (arrayOfChar != null)
      {
        arrayOfByte = new String(arrayOfChar).getBytes("UTF8");
        localPasswordCallback.clearPassword();
      }
      else
      {
        arrayOfByte = null;
      }
      return new Object[] { str3, arrayOfByte };
    }
    catch (IOException localIOException)
    {
      throw new SaslException("Cannot get password", localIOException);
    }
    catch (UnsupportedCallbackException localUnsupportedCallbackException)
    {
      throw new SaslException("Cannot get userid/password", localUnsupportedCallbackException);
    }
  }
}

/* Location:           D:\hygieia\userclient\common\libs\easemobchat_2.1.8.jar
 * Qualified Name:     org.apache.qpid.management.common.sasl.PlainSaslClient
 * JD-Core Version:    0.6.1
 */